package com.zhy.generator.codegen.mybatis.javahelper.elements;
import com.zhy.generator.codegen.dom.java.*;
import com.zhy.generator.codegen.mybatis.javahelper.JavaHelperMethodConstant;

import static com.zhy.generator.codegen.util.JavaBeansUtil.getCamelCaseString;

/**
 * @author 源
 */
public class PoToVOBatchMethodGenerator extends AbstractJavaHelperMethodGenerator {
    @Override
    public void addHelperElements(TopLevelClass tlc) {
        Method method = new Method(getMethodStr());
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setStatic(true);
        tlc.addMethod(method);

        FullyQualifiedJavaType paramListType = FullyQualifiedJavaType.getNewListInstance();
        FullyQualifiedJavaType paramType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        paramListType.addTypeArgument(paramType);

        String paramName = getCamelCaseString(paramType.getShortName(), false);
        Parameter p = new Parameter(paramListType, paramName);
        method.addParameter(p);
        tlc.addImportedType(paramType);
        tlc.addImportedType(paramListType);


        FullyQualifiedJavaType listType = FullyQualifiedJavaType.getNewListInstance();
        method.setReturnType(listType);
        FullyQualifiedJavaType voType = new FullyQualifiedJavaType(introspectedTable.getVoType());
        tlc.addImportedType(voType);
        method.setReturnType(voType);
        listType.addTypeArgument(voType);

        method.setReturnType(listType);

        tlc.addImportedType("com.google.common.collect.Lists");

        method.addBodyLine("List<"+voType.getShortName()+"> result = Lists.newArrayList();");
        method.addBodyLine(paramName+".forEach(po -> result.add(poToVO(po)));");
        method.addBodyLine(" return result;");

    }

    @Override
    public String getMethodStr() {
        return JavaHelperMethodConstant.PO_TO_VO_BATCH;
    }
}
